package com.labuladong;


import java.util.Objects;

/**
 * 项目名称: java_algorithm
 * @ClassName TheMostPathSum
 * @Description
 * @Author huipanxing huipanxing@chinasofti.com   @Date 2022/6/10 10:51
 * 1、左右每次深度递归都会维护ans值为最大子树和
 * 2、方法返回为当前节点和左右一边的最大和
 */
public class TheMostPathSum {

    static int ans=Integer.MIN_VALUE;

    public static void main(String[] args) {
        TreeNode treeNode=new TreeNode();
        treeNode.setLeftNode(new TreeNode(){{
            setTreeValue(1);
        }});
        treeNode.setRightNode(new TreeNode(){{
            setTreeValue(2);
        }});
        treeNode.setTreeValue(3);
        oneSideMax(treeNode);
        System.out.println(ans);
    }

    public static int oneSideMax(TreeNode treeNode){
        if (Objects.isNull(treeNode)){
            return 0;
        }
        int left=Math.max(0,oneSideMax(treeNode.getLeftNode()));
        int right=Math.max(0,oneSideMax(treeNode.getRightNode()));
        ans=Math.max(ans,left+right+treeNode.getTreeValue());
        return Math.max(left,right)+treeNode.getTreeValue();
    }
}
